/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package battleshipgame.ui;

import battleshipgame.logic.Battleship;
import battleshipgame.logic.ExitSignalException;
import battleshipgame.logic.InvalidInputException;
import battleshipgame.logic.Player;
import battleshipgame.manager.GameManager;
import battleshipgame.ui.ConsoleIOManager;

/**
 *
 * @author dunay
 */
public class ConsoleIOManualInputHandler {
        public static void getGameDataFromUser(GameManager gameManager)
    {
        System.out.println("Type q in any time to re enter user data");
        gameManager.totalPlayers = 0;
        int numOfPlayers = 0;
        try
        {
            try
            {
            numOfPlayers = ConsoleIOManager.inputInt("Please enter number of Players:");

            if(numOfPlayers < 2 || numOfPlayers > 6)
            {
                throw new InvalidInputException("Invalid numOfPlayers");
            }
            //gameManager.initPlayers(numOfPlayers);
            for(int i=0;i<numOfPlayers;i++)
            {
                String name = ConsoleIOManager.input("1 Player:\n\tName:\t");
                String type = ConsoleIOManager.input("\tPlayer Type[Type C for COMPUTER or H for HUMAN]:\t");
                // TODO: add computer\ human logic
                gameManager.addPlayer(new Player(name,i));
            }
             for(int i=0; i< gameManager.players.size(); i++)
                {
                    gameManager.players.get(i).initMarkBoards(numOfPlayers, gameManager.players);
                }
            }

            catch(InvalidInputException e)
            {
                System.out.println("Invalid Players Info:" + e.getMessage());
            }

            try
            {
                for(int i=0;i < numOfPlayers;i++)
                {
                    System.out.println(gameManager.getPlayer(i).getName() + " Battleships\n");

                    gameManager.getPlayer(i).initBattleshipList( Battleship.battleShipNamesList.length);

                    for(int j=0 ; j < Battleship.battleShipNamesList.length; j++)
                    {
                            Player.Direction direction = Player.Direction.NONE;
                            System.out.println("\t" + Battleship.battleShipNamesList[j] + ":");
                            String location =  ConsoleIOManager.input("\t\tLocation[for Example A1]:\t");
                            String directionStr = ConsoleIOManager.input("\t\tDirection[Type H for HORIZONTAL or V for VERTICAL]:");
                            if(directionStr.equals("H") || directionStr.equals("h"))
                            {
                                 direction = Player.Direction.HORIZONTAL;
                            }
                            else if (directionStr.equals("V") || directionStr.equals("v"))
                            {
                                direction = Player.Direction.VERTICAL;
                            }
                            else
                            {
                                //TODO handle invalid data inserted
                                ;
                            }

                            Battleship battleship = new Battleship(Battleship.battleShipNamesList[j], Battleship.battleShipSizeList[j]);
                            gameManager.getPlayer(i).SetBattleshipInBoard(battleship, location, direction);
                    }
                }
            }

            catch (InvalidInputException e)
            {
                //TODO re enter user on invalid
                System.out.println("Invalid Battleship data." + e.getMessage());
            }
        }
            catch(ExitSignalException e)
            {
                //re enter data
                getGameDataFromUser(gameManager);
            }
        //gameManager.initPlayers(numOfPlayers);
        }
}
